package com.hp.authweb.service.sercrity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Service;

import com.hp.authweb.AuthUser;
import com.hp.authweb.model.UserInfo;
import com.hp.authweb.service.UserService;
import com.hp.sso.service.SocialService;

/**
 * @Description:用户服务类
 * @author hp
 * @date 2018年5月20日 上午9:15:59
 */
@Service
public class UserDetailService implements UserDetailsService,SocialUserDetailsService {

	@Autowired
	private UserService userService;
	
	@Autowired
	private SocialService socialService;
	
	/**
	 * 常规页面登录
	 */
	@Override
	public UserDetails loadUserByUsername(String username) {
		
		AuthUser app = new AuthUser();
		UserInfo user = userService.queryUser(username);
		
		app.setUser(user);
		return app;
	}

	/**
	 * 三方登录获取用户用到
	 */
	@Override
	public SocialUserDetails loadUserByUserId(String userId){
		AuthUser app = new AuthUser();
		
		UserInfo user = null;
		try {
			user = userService.queryUser(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//获取不到用户就创建默认用户
		if (null == user) {
			user = socialService.creatUser(userId);
		}
		app.setUser(user);
		
		return app;
	}
	
} 
